<?php
use lib\controller\Controller;
use lib\dao\query\Query;
use lib\dao\query\QueryLimit;
use lib\dao\query\QueryLike;
use lib\dao\query\QueryAnd;
use lib\dao\query\QueryJoin;



class CategoriaController extends Controller{


// FRONTEND 
	
	public function index(){
		
		try{

			$Q = new Query($this->Model);

			//filtramos las categorias que tiene evento mediante el dao de categoria
			$this->View->assign('rows',  $this->Model->categoriasConEvento($Q));

			//Muestro template
			$this->View->layout = 'frontend';

			$this->View->display();
		}
		catch (\Exception $e){
			$this->View->assign('msg', array('error',$e->getMessage()));
		}
	}	

/*
BACKEND
*/	

	public function listado(){
		try{
			//$this->View->debug();
			$this->getMessages();
                        $search = $this->getParam('search');
                       
                        
                        $Q = new Query($this->Model);
                        if($search){
				$Q->add(new QueryLike('nombre',$search));
				//con un objeto
				/*
				$Q->add(new QueryLike(array('nombre'=>$this->getParam($search),
				'descripcion'=>$this->getParam('search'))));
				*/
			}	
			                  
                        //Asigno resultado a la vista
			$this->View->assign('rows', $this->Model->fetch());
			
						
			$P = new QueryLimit($this->getPage(),(int)$this->Config->pagination->rows);
			$Q->add($P);
			
			$Q->prepare($this->Model);	
			//$Q->dump();
			$this->View->assign('rows', $this->Model->fetch($Q));
			$this->View->assign('P', $P);
			$this->View->assign('page', $this->getPage());
			$this->View->assign('search', $search);
			//Muestro template
			$this->View->display();
                        
                        
		}
		catch (\Exception $e){
			$this->View->assign('msg', array('error',$e->getMessage()));
		}	
		
	}
	
	public function crear(){
		try{
			//hidden o tomar valor del boton submit para saber si
			//postearon formulario.
			$do_submit = $this->getParam('do_submit');
			if($do_submit){
				//cargo modelo con parametros del form
				$this->params2Model($this->Model);
				//campos obligatorios
				$this->required($this->Model, array('nombre'));
				//invoco create heredado de Model
				$this->Model->create();
				$this->clean($this->Model);
				$this->View->assign('msg', array('success', 'El registro se ha guardado correctamente'));
			}
		}
		catch (\Exception $e){
			$this->View->assign('msg', array('error',$e->getMessage()));
		}
		$this->View->assign('model', $this->Model->toArray());
		$this->View->display();	
	}
	
	public function eliminar(){		
			$id = $this->getParam('id');	
            $Q= new Query($this->Model);
			$Q->add(new QueryAnd('id',$id));
			
            if(empty($id)){
				$this->_helper->flashMessenger->addMessage('error');
                                $this->_helper->flashMessenger->addMessage('id vacio');
                                $this->_redirect("/categoria/listado", array('exit'=>true));
                               
			}
				
			$this->Model->id = $id;
			if(!$this->Model->load($Q)){
                               $this->_helper->flashMessenger->addMessage('error');
                                $this->_helper->flashMessenger->addMessage('error al eliminar categorìa');
                                $this->_redirect("/categoria/listado", array('exit'=>true));
			}
			
			$this->Model->delete();
                        
			$this->_helper->flashMessenger->addMessage('success');
                        $this->_helper->flashMessenger->addMessage('Categorìa eliminada');
                        $this->_redirect("/categoria/listado", array('exit'=>true));		
	}
	
	public function actualizar(){           
                try{
			//hidden o tomar valor del boton submit para saber si
			//postearon formulario.
			$do_submit 	= $this->getParam('do_submit');
			$id 		= $this->getParam('id');

			if(empty($id)){
				throw new \Exception('No es posible continuar sin identificador');
			}
			
			$this->Model->id = $id;
                        $Q= new Query($this->Model);
			$Q->add(new QueryAnd('id',$id));
                        
			if(!$this->Model->load($Q)){
				throw new \Exception('Identificador no valido');
			}
			
			if($do_submit){
				//cargo modelo con parametros del form
				$this->params2Model($this->Model);
				//campos obligatorios
				$this->required($this->Model, array('nombre'));
				//invoco update heredado de Model
				$this->Model->update();
                                header("location:/categoria/listado");
				$this->View->assign('msg', array('success', 'El registro se ha actualizado correctamente'));
			}
		}
		catch (\Exception $e){
			$this->View->assign('msg', array('error',$e->getMessage()));
		}
		$this->View->assign('id', $id);
		$this->View->assign('model', $this->Model->toArray());
		$this->View->display();
	}
}